﻿Imports ACompany.OrderProcessing.Services
Imports ACompany.OrderProcessing.Model
Imports StructureMap

Public Class OrderListPresenter

    Private _view As IOrderListView
    Private _OrderService As IOrderService

    Public Sub New(ByVal view As IOrderListView)
        Me.New(view, ObjectFactory.GetInstance(Of IOrderService)())
    End Sub

    Public Sub New(ByVal view As IOrderListView, ByVal orderService As IOrderService)
        _view = view
        _OrderService = orderService
    End Sub

    Public Sub Display()

        clearInfoAndErrorMessages()

        Dim orders As List(Of Order)
        orders = _OrderService.GetAllOrdersBy(_view.OrderStatus)

        UpdateViewFrom(orders)

    End Sub

    Private Sub UpdateViewFrom(ByVal orders As List(Of Order))
        _view.Orders = orders
        displayInformationFrom(orders)
    End Sub

    Private Sub displayInformationFrom(ByVal orders As List(Of Order))
        clearInfoAndErrorMessages()

        If orders.Count = 0 Then
            _view.InfoMessage(String.Format("There are no orders with the status '{0}'.", _view.OrderStatus.ToString))
        End If
    End Sub

    Private Sub clearInfoAndErrorMessages()
        _view.ErrorMessage(String.Empty)
        _view.InfoMessage(String.Empty)
    End Sub

End Class
